package com.nanuminc.market.utils;

import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import java.util.regex.Pattern;

import android.util.Log;


public abstract class StringUtil {

	public static final String EMPTY = "";

	private StringUtil() {
	}

	public static String encodePassword(String password, String algorithm) {
		byte unencodedPassword[] = password.getBytes();
		MessageDigest md = null;
		try {
			md = MessageDigest.getInstance(algorithm);
		} catch (Exception e) {
			Log.d("nanum","Exception: "+e.getMessage().toString());
			return password;
		}
		md.reset();
		md.update(unencodedPassword);
		byte encodedPassword[] = md.digest();
		StringBuffer buf = new StringBuffer();
		for (int i = 0; i < encodedPassword.length; i++) {
			if ((encodedPassword[i] & 255) < 16)
				buf.append("0");
			buf.append(Long.toString(encodedPassword[i] & 255, 16));
		}

		return buf.toString();
	}


	public static String swapFirstLetterCase(String str) {
		StringBuffer sbuf = new StringBuffer(str);
		sbuf.deleteCharAt(0);
		if (Character.isLowerCase(str.substring(0, 1).toCharArray()[0]))
			sbuf.insert(0, str.substring(0, 1).toUpperCase());
		else
			sbuf.insert(0, str.substring(0, 1).toLowerCase());
		return sbuf.toString();
	}

	public static String trim(String origString, String trimString) {
		int startPosit = origString.indexOf(trimString);
		if (startPosit != -1) {
			int endPosit = trimString.length() + startPosit;
			return (new StringBuilder())
					.append(origString.substring(0, startPosit))
					.append(origString.substring(endPosit)).toString();
		} else {
			return origString;
		}
	}

	public static String getLastString(String origStr, String strToken) {
		StringTokenizer str = new StringTokenizer(origStr, strToken);
		String lastStr;
		for (lastStr = ""; str.hasMoreTokens(); lastStr = str.nextToken())
			;
		return lastStr;
	}

	public static String[] getStringArray(String str, String strToken) {
		if (str.indexOf(strToken) != -1) {
			StringTokenizer st = new StringTokenizer(str, strToken);
			String stringArray[] = new String[st.countTokens()];
			for (int i = 0; st.hasMoreTokens(); i++)
				stringArray[i] = st.nextToken();

			return stringArray;
		} else {
			return (new String[] { str });
		}
	}

	public static boolean isNotEmpty(String str) {
		return !isEmpty(str);
	}

	public static boolean isEmpty(String str) {
		return str == null || str.length() == 0;
	}

	public static String replace(String str, String replacedStr,
			String replaceStr) {
		String newStr = "";
		if (str.indexOf(replacedStr) != -1) {
			String s1 = str.substring(0, str.indexOf(replacedStr));
			String s2 = str.substring(str.indexOf(replacedStr) + 1);
			newStr = (new StringBuilder()).append(s1).append(replaceStr)
					.append(s2).toString();
		}
		return newStr;
	}

	public static int string2integer(String str) {
		int ret = Integer.parseInt(str.trim());
		return ret;
	}

	public static String integer2string(int integer) {
		return (new StringBuilder()).append("").append(integer).toString();
	}

	public static boolean isPatternMatching(String str, String pattern)
			throws Exception {
		if (pattern.indexOf('*') >= 0)
			pattern = pattern.replaceAll("\\*", ".*");
		pattern = (new StringBuilder()).append("^").append(pattern).append("$")
				.toString();
		return Pattern.matches(pattern, str);
	}

	public static boolean containsMaxSequence(String str, String maxSeqNumber) {
		int occurence = 1;
		int max = string2integer(maxSeqNumber);
		if (str == null)
			return false;
		int sz = str.length();
		for (int i = 0; i < sz - 1; i++)
			if (str.charAt(i) == str.charAt(i + 1)) {
				if (++occurence == max)
					return true;
			} else {
				occurence = 1;
			}

		return false;
	}

	public static boolean containsInvalidChars(String str, char invalidChars[]) {
		if (str == null || invalidChars == null)
			return false;
		int strSize = str.length();
		int validSize = invalidChars.length;
		for (int i = 0; i < strSize; i++) {
			char ch = str.charAt(i);
			for (int j = 0; j < validSize; j++)
				if (invalidChars[j] == ch)
					return true;

		}

		return false;
	}

	public static boolean containsInvalidChars(String str, String invalidChars) {
		if (str == null || invalidChars == null)
			return true;
		else
			return containsInvalidChars(str, invalidChars.toCharArray());
	}

	public static boolean isAlphaNumeric(String str) {
		if (str == null)
			return false;
		int sz = str.length();
		if (sz == 0)
			return false;
		for (int i = 0; i < sz; i++)
			if (!Character.isLetterOrDigit(str.charAt(i)))
				return false;

		return true;
	}

	public static boolean isAlpha(String str) {
		if (str == null)
			return false;
		int sz = str.length();
		if (sz == 0)
			return false;
		for (int i = 0; i < sz; i++)
			if (!Character.isLetter(str.charAt(i)))
				return false;

		return true;
	}

	public static boolean isNumeric(String str) {
		if (str == null)
			return false;
		int sz = str.length();
		if (sz == 0)
			return false;
		for (int i = 0; i < sz; i++)
			if (!Character.isDigit(str.charAt(i)))
				return false;

		return true;
	}

	public static String reverse(String str) {
		if (str == null)
			return null;
		else
			return (new StringBuffer(str)).reverse().toString();
	}

	public static String fillString(String originalStr, char ch, int cipers) {
		int originalStrLength = originalStr.length();
		if (cipers < originalStrLength)
			return null;
		int difference = cipers - originalStrLength;
		StringBuffer strBuf = new StringBuffer();
		for (int i = 0; i < difference; i++)
			strBuf.append(ch);

		strBuf.append(originalStr);
		return strBuf.toString();
	}

	public static final boolean isEmptyTrimmed(String foo) {
		return foo == null || foo.trim().length() == 0;
	}

	public static List<String> getTokens(String lst, String separator) {
		List<String> tokens = new ArrayList<String>();
		if (lst != null) {
			for (StringTokenizer st = new StringTokenizer(lst, separator); st
					.hasMoreTokens();)
				try {
					String en = st.nextToken().trim();
					tokens.add(en);
				} catch (Exception e) {
					Log.d("nanum","String tokening error occurs. Error : "+	e.getMessage().toString());
				}

		}
		return tokens;
	}

	public static List<String> getTokens(String lst) {
		return getTokens(lst, ",");
	}

	public static String convertToCamelCase(String targetString, char posChar) {
		StringBuffer result = new StringBuffer();
		boolean nextUpper = false;
		String allLower = targetString.toLowerCase();
		for (int i = 0; i < allLower.length(); i++) {
			char currentChar = allLower.charAt(i);
			if (currentChar == posChar) {
				nextUpper = true;
				continue;
			}
			if (nextUpper) {
				currentChar = Character.toUpperCase(currentChar);
				nextUpper = false;
			}
			result.append(currentChar);
		}

		return result.toString();
	}

	public static String convertToCamelCase(String underScore) {
		return convertToCamelCase(underScore, '_');
	}

	public static String convertToUnderScore(String camelCase) {
		String result = "";
		for (int i = 0; i < camelCase.length(); i++) {
			char currentChar = camelCase.charAt(i);
			if (i > 0 && Character.isUpperCase(currentChar))
				result = result.concat("_");
			result = result.concat(Character.toString(currentChar)
					.toLowerCase());
		}

		return result;
	}

	public static String null2str(String org, String converted) {
		if (org == null || org.trim().length() == 0)
			return converted;
		else
			return org.trim();
	}

	public static String null2str(String org) {
		return null2str(org, "");
	}

}